home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / archivers / xpk / xpk_source / examples / xsum2.c < prev   
C/C++ Source or Header  |  1999-06-14  |  1KB  |  67 lines

  1. /* XSum2.c - sums up all bytes in a compressed or uncompressed file
  2.  *           without reading the whole file at once into mem
  3.  *
  4.  * This is a typical read-and-process xpk application. Try it out... XSum a
  5.  * file, then compress it and XSum it again. The result should be the same.
  6.  */
  7.  
  8. #include <proto/exec.h>
  9. #include <proto/dos.h>
  10. #include <proto/xpkmaster.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. extern struct Library *DOSBase;
  16. struct Library *XpkBase = NULL;
  17.  
  18. char errbuf[XPKERRMSGSIZE + 1];
  19. UBYTE *outbuf = NULL;
  20. long outbuflen;
  21.  
  22. void end (char *text)
  23. {
  24.   if (outbuf)
  25.     FreeMem (outbuf, outbuflen);
  26.   if (text)
  27.     Write (Output (), text, strlen (text));
  28.   if (XpkBase)
  29.     CloseLibrary (XpkBase);
  30.   exit (text ? 10 : 0);
  31. }
  32.  
  33. void 
  34. main (int argc, char *argv[])
  35. {
  36.   struct XpkFib *xfh;
  37.   LONG len, sum = 0;
  38.   UBYTE *ptr, *last;
  39.  
  40.   if(argc != 2)
  41.     end("Usage: xSum <filename>\n");
  42.  
  43.   if(!(XpkBase = OpenLibrary (XPKNAME, 0)))
  44.     end("Cannot open " XPKNAME "\n");
  45.  
  46.   if(XpkOpenTags (&xfh, XPK_InName, argv[1], XPK_PassThru, TRUE, TAG_DONE))
  47.     end(strcat (errbuf, "\n"));
  48.  
  49.   if(!(outbuf = (UBYTE *) AllocMem (outbuflen = xfh->xf_NLen + XPK_MARGIN, 0)))
  50.     end("Out of memory\n");
  51.  
  52.   while((len = XpkRead (xfh, outbuf, XPKLEN_ONECHUNK)) > 0)
  53.   {
  54.     ptr = (UBYTE *) outbuf;
  55.     last = ptr + len;
  56.     while (ptr < last)
  57.       sum += *ptr++;
  58.   }
  59.  
  60.   if(XpkClose (xfh) || len)
  61.     end (strcat (errbuf, "\n"));
  62.  
  63.   printf("%d\n", sum);
  64.  
  65.   end (NULL);
  66. }
  67.